package com.yjlove.myplay.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.viewpagerindicator.TabPageIndicator;
import com.yjlove.myplay.R;
import com.yjlove.myplay.ui.fragment.TabFragment;

/**
 * 作者 YJlvoe
 * 创建时间 2016/12/29.
 */
public class VPIndicatorTestActivity  extends FragmentActivity {

    /**
     * Tab标题
     */
    private static final String[] title = new String[] { "订单统计", "地块统计", "飞手统计", "设备统计" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vptest);

        initView();
    }
    private void initView() {
        ViewPager viewpager = (ViewPager) findViewById(R.id.viewpager);
        TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
        TabAdapter adapter = new TabAdapter(getSupportFragmentManager());
        viewpager.setAdapter(adapter);
        indicator.setViewPager(viewpager);
        //这里是为了清楚TabPageIndicator时报的ViewPager has not been bound异常，先给TabPageIndicator设置android:visibility="gone"
        indicator.setVisibility(View.VISIBLE);
    }

    /**
     * 适配器代码
     */
    class TabAdapter extends FragmentPagerAdapter {


        public TabAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = new TabFragment();
            Bundle args = new Bundle();
            args.putString("args",title[position]);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount() {
            return title.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }
    }
}
